package wsepForum.ui.jaxcent.states;

import java.util.Map;

import jaxcent.HtmlInputButton;
import jaxcent.HtmlInputText;
import jaxcent.Jaxception;
import wsepForum.ui.jaxcent.IXContext;

/**
 * @author Oren
 *
 */
public class XLogin implements IXState {
	/*****************************
	 * MEMBERS                   *
	 *****************************/
	private IXContext context;

	/*****************************
	 * CONSTRUCTOR               *
	 *****************************/
	public XLogin(IXContext context) {
		this.context = context;
		initContent();
	}


	/*****************************
	 * METHODS                   *
	 *****************************/
	/* (non-Javadoc)
	 * @see wsepForum.network.server.jax.pages.states.XState#getType()
	 */
	@Override
	public StateType getType() {
		return StateType.LOGIN;
	}


	private void initContent() {
		construct();
		new HtmlInputButton( this.context.getPage(), "xloginsubmit" ) {
		        @SuppressWarnings("unchecked")
				protected void onClick(Map pageData) {
		        		String userName = (String) pageData.get( "xloginuser" );
		                String password = (String) pageData.get("xloginpass");
		                if ( userName.equals( "" ) || password.equals( "" )) {
		                	context.getPage().showMessageDialog( "Please enter username and password" );
		                    return;
		                }
		                context.loginUser(userName,password);
		        }
		    };
		    try {
				new HtmlInputText (this.context.getPage(),"xloginuser").focus();
			} catch (Jaxception e) {
				this.context.getPage().showMessageDialog("cannot comunnicate with page fields!");
				this.context.loadSection(0);
			}
	}
	private void construct() {
		this.context.setBody(
		"<form id=\"login\"><div class=\"panel\">"+
		"<div class=\"inner\"><span class=\"corners-top\"><span></span></span>"+
		"<div class=\"content\"><h2>Login</h2>"+
		"<fieldset class=\"fields1\"><dl>"+
		"<dt><label for=\"username\">Username:</label></dt><dd>"+
		"<input type=\"text\" tabindex=\"1\" name=\"xloginuser\" id=\"xloginuser\" size=\"25\" value=\"\" class=\"inputbox autowidth\" /></dd>"+
		"</dl><dl><dt><label for=\"password\">Password:</label></dt><dd>"+
		"<input type=\"password\" tabindex=\"2\" id=\"xloginpass\" name=\"xloginpass\" size=\"25\" class=\"inputbox autowidth\" /></dd>"+
		"</dl><dl><dt>&nbsp;</dt><dd>"+
		"<input type=\"button\" name=\"xloginsubmit\" id=\"xloginsubmit\" tabindex=\"6\" value=\"Login\" class=\"button1\" /></dd>"+
		"</dl></fieldset></div><span class=\"corners-bottom\"><span></span></span></div></div></form>"
		);
	}


}
